import xlrd
import xlwt
from  xlutils.copy import copy



class  OperateExcel:

    def __init__(self,filename=None):

        if filename is None:
            self.filename = "test1.xls"
        else:
            self.filename = filename
        self.data = self.read()


    def read(self):
        data = xlrd.open_workbook(self.filename)
        table = data.sheet_by_index(0)

        return table

    def rrows(self):
        rows = self.data.nrows
        print(rows)

    def rcols(self):
        cols = self.data.ncols
        print(cols)

    def read_cell(self,row,col):
        cell = self.data.cell_value(row,col)
        print(cell)
        return cell


    def write_cell(self,row,col,value):
        read_data = xlrd.open_workbook(self.filename)
        write_data = copy(read_data)
        sheet_data = write_data.get_sheet(0)
        sheet_data.write(row,col,value)
        write_data.save(self.filename)





if __name__ == '__main__':

    # file1 = xlrd.open_workbook("test1.xls")
    # table = file1.sheets()[0]
    # rows = table.nrows
    # cols = table.ncols
    # print(rows,cols)
    # value = table.cell_value(0,0)
    # print(value)

    # write_data = copy(file1)
    # sheet_data = write_data.get_sheet(0)
    # sheet_data.write(1,3,"test1")
    # write_data.save("test1.xls")



    file = OperateExcel()
    file.rrows()
    file.rcols()
    file.read_cell(1,0)
    file.write_cell(1,5,'wt')
